package com.elong.android.specialhouse.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.ApartmentAPI;
import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.adapter.ApartmentCommentListAdapter;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.entity.response.CommentResp;
import com.elong.android.specialhouse.entity.response.HouseCommentltem;
import com.elong.android.specialhouse.request.GetCommentListReq;
import com.elong.android.specialhouse.ui.FlexibleRatingBar;
import com.elong.framework.netmid.ElongRequest;
import com.elong.framework.netmid.response.IResponse;
import com.elong.framework.netmid.response.StringResponse;

import java.util.List;

/**
 * 
 * @ClassName: ApartmentCommentActivity
 * @Description: (公寓更多评价界面)
 * @author lin.chai
 * @date 2015-08-10
 */

public class ApartmentCommentActivity extends BaseVolleyActivity<IResponse<?>>
		implements OnScrollListener {
	private FlexibleRatingBar mScoreRatingBar;
	private TextView mScoreTextView;
	private ViewSwitcher mCommentsViewSwitcher;
	private ListView mCommentsListView;
	private View mCommentsListFooter;
	private ApartmentCommentListAdapter mCommentListAdapter;
	private List<HouseCommentltem> mCommentsList;
	private int mCommentsCount = 0;
	private boolean isLastRow = false;
	private boolean isRefresing = false;
	private int mOldCommentListTop = 0;
	private int mOldCommentListSelection = 0;
	private Long mHouseId;

	@Override
	protected void initContentView() {
		setContentView(R.layout.act_apartment_comment);
		setHeader(R.string.all_comments);
		mScoreRatingBar = (FlexibleRatingBar) findViewById(R.id.score_ratingbar);
		mScoreTextView = (TextView) findViewById(R.id.score_text);
		mCommentsViewSwitcher = (ViewSwitcher) findViewById(R.id.apartment_detail_commentswitcher);
		mCommentsListView = (ListView) findViewById(R.id.apartment_detail_comments);
		mCommentsListFooter = LayoutInflater.from(this).inflate(
				R.layout.item_comment_list_footer, null);
		mCommentsListFooter.setVisibility(View.VISIBLE);
		mCommentsListView.addFooterView(mCommentsListFooter);
		((TextView) mCommentsListFooter.findViewById(R.id.list_footer))
				.setText(R.string.more_comments);
		mCommentsListView.setOnScrollListener(this);

		// 获取房源id
		mHouseId = getIntent().getLongExtra(AppConstants.ACTIVITY_KEY_HOUSE_ID,
				0);

		// 获取评论数据
		getComments();

	}

	private void getComments() {
		int pageIndex = 1;
		if (mCommentsList != null && mCommentsList.size() >= 1) {
			pageIndex = (mCommentsList.size() - 1)
					/ AppConstants.COMMENTS_PAGESIZE + 2;
		} else {
			pageIndex = 1;
		}
		GetCommentListReq req = new GetCommentListReq();
		req.HouseId = mHouseId;
		req.PageNumber = pageIndex;
		req.PageSize = AppConstants.COMMENTS_PAGESIZE;
		req.TagType = 7;
		requestHttp(req, ApartmentAPI.getCommentList, StringResponse.class,
				true);

	}

	@Override
	public void onTaskPost(ElongRequest request, IResponse<?> t) {
		super.onTaskPost(request, t);
		JSONObject result = (JSONObject) JSONObject.parse(((StringResponse) t)
				.getContent());
		if (!handleIsError(request, result)) {
			CommentResp response = JSON.parseObject(t.toString(),
					CommentResp.class);
			mCommentsCount = response.getCountComment();
			mScoreRatingBar.setRating(response.getAvgStar());
			mScoreTextView.setText(Float.toString(response.getAvgStar())
					+ getString(R.string.comment_score));
			List<HouseCommentltem> comments = response.getcomments();

			// 获取数据后处理显示
			if (comments != null && mCommentsCount > 0) {
				// 有评价显示列表
				mCommentsViewSwitcher.setDisplayedChild(0);
				if (null == mCommentListAdapter) {
					mCommentsList = comments;
					mCommentListAdapter = new ApartmentCommentListAdapter(
							mCommentsList);
					mCommentsListView.setAdapter(mCommentListAdapter);
				} else {
					mCommentsList.addAll(comments);
					mCommentListAdapter.notifyDataSetChanged();
					((ListView) this.mCommentsListView).setSelectionFromTop(
							mOldCommentListSelection, mOldCommentListTop);
				}
				if (!hasMoreItems()) {
					mCommentsListView.removeFooterView(mCommentsListFooter);
				}

				if (mCommentsList.isEmpty()) {
					mCommentsViewSwitcher.setDisplayedChild(1);
				}

			} else {
				// 没有评价显示提示
				mCommentsViewSwitcher.setDisplayedChild(1);
			}
		}
		isRefresing = false;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (hasMoreItems() && totalItemCount > 0
				&& (firstVisibleItem + visibleItemCount >= totalItemCount)
				&& !isRefresing) {
			isLastRow = true;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (isLastRow
				&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			mOldCommentListSelection = view.getFirstVisiblePosition();
			View firstItem = view.getChildAt(0);
			mOldCommentListTop = (firstItem == null ? 0 : firstItem.getTop());
			isLastRow = false;
			isRefresing = true;
			getComments();
		}
	}

	private boolean hasMoreItems() {
		if (mCommentsList != null) {
			return mCommentsList.size() < mCommentsCount;
		} else {
			return false;
		}

	}

}
